package com.solution.admin.order.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.order.entity.OrderContractEntity;
import com.solution.admin.order.service.OrderContractService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.utils.string.StringUtil;

/**
 * 订单合同web控制层
 * @author lilp
 * @date 2017-09-18 19:23:07
 */
@RequestMapping("/orderContract")
@Controller
public class OrderContractController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(OrderContractController.class);

	@Autowired
	private OrderContractService orderContractService;
	
	/**
	 * 查询订单合同列表
	 * @param orderContract 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2017-09-18 19:23:07
	 */
	@RequestMapping("/listOrderContract")
	@ResponseBody
	public OrderContractEntity listOrderContractPage(HttpServletRequest request){
		OrderContractEntity contractEntity = null;
		String orderCode = request.getParameter("orderCode");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orderCode", orderCode);
		try {
			List<String> contractImageList = new ArrayList<String>();
			List<OrderContractEntity>  orderContractList = orderContractService.listOrderContract(paramMap);
			if(orderContractList != null && orderContractList.size() > 0){
				contractEntity = orderContractList.get(orderContractList.size()-1);
				if(StringUtil.isNotEmpty(contractEntity.getFilePath())){
					String[] filePathArr = contractEntity.getFilePath().split(",");
					for (String filePath : filePathArr) {
						contractImageList.add(filePath);
					}
					contractEntity.setImageList(contractImageList);
				}
			}
		} catch (Exception e) {
			handleException("查询订单合同失败", logger, e);
		}
		return contractEntity;
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-09-18 19:23:07
	 */
	@RequestMapping("/toAddOrderContract")
	public String toAddOrderContract(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "order/orderContract_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-09-18 19:23:07
	 */
	@RequestMapping("/toUpdateOrderContract")
	public String toUpdateOrderContract(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			OrderContractEntity orderContract = orderContractService.getOrderContractById(id);
			request.setAttribute("orderContract", orderContract);
		} catch (Exception e) {
			handleException("跳转编辑订单合同页面错误", logger, e);
		}
		return "order/orderContract_form";
	}


	/**
	 * 新增或保存订单合同
	 * @param request 请求对象
	 * @param orderContractEntity 订单合同实体
	 * @return
	 * @author lilp
 	 * @date 2017-09-18 19:23:07
	 */
	@RequestMapping("/saveOrUpdateOrderContract")
	@ResponseBody
	public Map<String, Object> saveOrderContract(HttpServletRequest request, OrderContractEntity orderContractEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isEmpty(id)){
				result = orderContractService.updateOrderContract(orderContractEntity);
			}else{
				result = (int)orderContractService.saveOrderContract(orderContractEntity);
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加订单合同失败", logger, e);
		}
		return resultMap;
	}
	
}
